package com.qfedu.lc.user.controller;


import com.qfedu.lc.common.config.SystemConfig;
import com.qfedu.lc.common.param.UserAdd;
import com.qfedu.lc.common.param.UserLogin;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 1.用户表 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-06-05
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public R register(HttpServletRequest request, @RequestBody UserAdd userAdd){

       return userService.register(request,userAdd);
    }

    /**
     * 使用 账号 + 密码登录
     * 1.判断参数是否为空
     * 2.判断账户是存在，可用
     * 3.判断登录次数是否超过三次
     * 4.验证用户 密码
     *
     * 5.生成token
     * 6.存储token
     * 7.记录登录日志
     * 8.返回token
     *
     * @return
     */
    @RequestMapping("/loginpass")
    public R loginPass(@RequestBody UserLogin userLogin){


        return userService.loginPass(userLogin);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public R logout(HttpServletRequest request){

        String token = request.getHeader(SystemConfig.HEADER_TOKEN);

       return userService.logout(token);

    }

}

